﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Linq;

namespace GhostGame.DataObjects
{
    public class GhostRoom
    {
        public enum RoomStatus
        {
            Waiting,
            GhostHunting,
            End
        }

        public static GhostRoom CreateRoom()
        {
            GhostRoom room = new GhostRoom();
            GameContainer.Rooms.Add(room);
            room.ID = GameContainer.Rooms.Max(r => r.ID) + 1;
            room.Name = "新房间" + DateTime.Now.ToShortTimeString();
            room.UpdateTime();
            return room;
        }

        public void UpdateTime()
        {
            this.LastUpdatedTime = DateTime.Now;
        }

        public string HolderName
        {
            get
            {
                if (this.Holder != null)
                {
                    return this.Holder.Name;
                }
                else
                {
                    return "未指定";
                }
            }
        }
        public DateTime LastUpdatedTime = DateTime.Now;
        public string Name {get;set;}
        public List<User> Members = new List<User>();
        public User Holder;
        public string HumanWord = "人";
        public string GhostWord = "鬼";
        public RoomStatus Status = RoomStatus.Waiting;
        public int ID;

        public void ClearRoom()
        {
            this.Status = RoomStatus.End;
            this.LastUpdatedTime = DateTime.Now;
            foreach (var item in this.Members)
            {
                item.ClearStatus();
            }
            this.Members.Clear();
            this.Holder = null;
            this.HumanWord = "";
            this.GhostWord = "";
        }

        public void RemoveMember(int UserID)
        {
            User u = this.Members.FirstOrDefault(user => user.ID == UserID);
            if (u != default(User))
            {
                u.ClearStatus();
                this.Members.Remove(u);
            }
        }

        public void AddMember(int UserID)
        {
            if (this.Members.Select(u => u.ID).Contains(UserID))
            {
                return;
            }
            else if (this.Holder.ID == UserID)
            {
                return;
            }
            else
            {
                this.Members.Add(GameContainer.Users.First(u => u.ID == UserID));
            }
        }

        public void SwitchJudge(int UserID)
        {
            if (this.Holder.ID == UserID)
            {
                return;
            }
            else if (this.Members.Select(u => u.ID).Contains(UserID))
            {
                this.Holder.ClearStatus();
                this.Members.Add(this.Holder);
                this.Holder = this.Members.First(u => u.ID == UserID);
                this.Holder.ClearStatus();
                this.RemoveMember(UserID);
            }
            else
            {
                throw new ArgumentException();
            }
        }
    }
}